//
//  CZNewFeatureController.m
//  OCKSFWeibo
//
//  Created by 盛 发 况 on 16/4/20.
//  Copyright © 2016年 盛 发 况. All rights reserved.
//

#import "CZNewFeatureController.h"
#import "NewFeatureCell.h"
#define CZNewFeatureCount 4

@interface CZNewFeatureController ()
@property (nonatomic, weak) UIPageControl *pageControl;

@end

@implementation CZNewFeatureController
static NSString *const ID = @"NewFeatureCell";

- (instancetype)init {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = [UIScreen mainScreen].bounds.size;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    return [super initWithCollectionViewLayout:flowLayout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //注册cell
    [self.collectionView registerClass:[NewFeatureCell class] forCellWithReuseIdentifier:ID];
    
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    //self.collectionView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
    
    //添加控件pageControl
    [self setUpPageControl];
}
//添加控件pageControl
- (void)setUpPageControl {

    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = CZNewFeatureCount;
    pageControl.pageIndicatorTintColor = [UIColor blackColor];
    pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    
    //设置frame
    pageControl.center = CGPointMake(self.view.width * 0.5, self.view.height * 0.95);
    
    _pageControl = pageControl;
    [self.view addSubview:pageControl];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    int page = scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5;

    self.pageControl.currentPage = page;
}

#pragma mark --- 数据源
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return CZNewFeatureCount;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    NewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    NSString *imageName = [NSString stringWithFormat:@"new_feature_%zd",indexPath.item + 1];
    cell.image = [UIImage imageNamed:imageName];
    
    // 判断是否是最后一页
    [cell selectIndexPath:indexPath count:CZNewFeatureCount];
    
    return cell;
}


@end






